<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 180px;"
        class="filter-item"
        placeholder="请输入交易编码"
        v-model="queryForm.voucherCode"
      >
      </el-input>
      <el-input
        style="width: 180px;"
        class="filter-item"
        placeholder="请输入用户手机号"
        v-model="queryForm.phone"
      >
      </el-input>
      <el-input
        style="width: 180px;"
        class="filter-item"
        placeholder="请输入外部交易号"
        v-model="queryForm.externalTransno"
      >
      </el-input>
      <el-select
        clearable
        style="width: 150px"
        class="filter-item"
        v-model="queryForm.transCode"
        placeholder="请选择交易类型"
      >
        <el-option
          v-for="(val, key) in transCodeOptions"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        >查询</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="voucherCode"
        label="交易编号"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="用户昵称"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="150" align="center">
      </el-table-column>
      <el-table-column
        prop="transCode"
        label="交易类型"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.TRANS_CODE[scope.row.transCode] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="transAmt"
        sortable="custom"
        sort-by="trans_amt"
        label="交易金额"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="payType"
        sortable="custom"
        sort-by="pay_type"
        label="支付方式"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.PAY_TYPE[scope.row.payType] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="externalTransno"
        label="外部交易号"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="transDate"
        sortable="custom"
        sort-by="trans_date"
        label="交易时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="payTime"
        label="支付时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        class-name="small-padding"
        width="200"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-permission="'trans:detail'"
            size="mini"
            type="success"
            @click="handleQueryById(scope.row, 'published')"
            icon="el-icon-search"
            circle
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <!-- 详情弹出框 -->
    <el-dialog
      :title="`交易详情`"
      :visible.sync="dialogShowFormVisible"
      width="50%"
    >
      <el-form :model="showForm" label-width="120px" ref="showForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="交易号:" prop="voucherCode">
              {{ showForm.voucherCode }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="外部交易号:" prop="externalTransno">
              {{ showForm.externalTransno }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户昵称:" prop="nickname">
              {{ showForm.nickname }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号:" prop="phone">
              {{ showForm.phone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="交易类型:" prop="transCode">
              <template slot-scope="scope">
                {{ Dicts.TRANS_CODE[showForm.transCode] }}
              </template>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付方式:" prop="payType">
              <template slot-scope="scope">
                {{ Dicts.PAY_TYPE[showForm.payType] }}
              </template>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="交易时间:" prop="transDate">
              {{ showForm.transDate }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付时间:" prop="payTime">
              {{ showForm.payTime }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogShowFormVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryBusiVoucher, queryBusiVoucherDetail } from '@/api/trade'

export default {
  name: 'busiVoucher',
  data() {
    return {
      loading: false,
      dialogShowFormVisible: false,
      showForm: [],
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        importance: undefined,
        title: undefined
      },
      transCodeOptions: Dicts.TRANS_CODES,
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryBusiVoucher(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    handleQueryById({ rowId }) {
      queryBusiVoucherDetail(rowId).then(({ data: { code, data, msg } }) => {
        if (code === 0) {
          this.openShowDialog()
          this.showForm = data
        }
      })
    },
    openShowDialog() {
      this.showForm = {}
      this.dialogShowFormVisible = true
      this.$refs.showForm && this.$refs.showForm.resetFields()
    }
  },
  components: {}
}
</script>

<style scoped></style>
